package com.lucun.insurance.service.Impl;

import com.lucun.insurance.db.entity.InsureEntryChild;
import com.lucun.insurance.db.entity.InsureEntryStaff;
import com.lucun.insurance.db.mapper.InsureEntryChildMapper;
import com.lucun.insurance.db.mapper.InsureEntryStaffMapper;
import com.lucun.insurance.service.InsureEntryChildService;
import com.lucun.insurance.util.BaseException;
import com.lucun.insurance.util.JsonResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 郭鑫
 * @catalogue: com.lucun.insurance.service.Impl
 * @Date: 2021/9/9 17:13
 * @Version: 1.0
 */
@Service
public class InsureEntryChildServiceImpl implements InsureEntryChildService {
    @Autowired
    InsureEntryChildMapper insureEntryChildMapper;
    @Autowired
    InsureEntryStaffMapper insureEntryStaffMapper;

    //    查询所有
    @Override
    public List<InsureEntryChild> findAll() {
        return insureEntryChildMapper.findAll();
    }

    //添加
    @Override
    public JsonResult<?> insert(InsureEntryChild insureEntryChild) {
        if (ObjectUtils.isEmpty(insureEntryChild)) {
            throw new BaseException("添加数据不能为空");
        }
        String name = insureEntryChild.getName();
        if (StringUtils.isBlank(name)) {
            throw new BaseException("录入类型不能为空");
        }
        String fatherNumber = insureEntryChild.getFatherNumber();
        if (StringUtils.isBlank(fatherNumber)) {
            throw new BaseException("父亲员工编码不能为空");
        }
        String motherNumber = insureEntryChild.getMotherNumber();
        if (StringUtils.isBlank(motherNumber)) {
            throw new BaseException("母亲员工编码不能为空");
        }
        int underwriting = insureEntryChild.getUnderwriting();
        if (ObjectUtils.isEmpty(underwriting)) {
            throw new BaseException("承保情况不能为空");
        }
        Long insureId = insureEntryChild.getInsureId();
        if (ObjectUtils.isEmpty(insureId)) {
            throw new BaseException("被保人id不能为空");
        }
        InsureEntryChild insureEntryChild1 = insureEntryChildMapper.selectById(insureEntryChild.getInsureId());
        if (!ObjectUtils.isEmpty(insureEntryChild1)) {
            throw new BaseException("该员工已添加录入类型独生子女类型");
        }
        int i = insureEntryChildMapper.insert(insureEntryChild);
        if (i != 0) {
            return new JsonResult<>("200");
        }
        return new JsonResult<>("500");
    }

    //修改
    @Override
    public JsonResult<?> update(InsureEntryChild insureEntryChild) {
        if (ObjectUtils.isEmpty(insureEntryChild)) {
            throw new BaseException("没有获取数据");
        }
        int i = insureEntryChildMapper.updateByPrimaryKeySelective(insureEntryChild);
//        InsureEntryChild insureEntrChild1 = insureEntrChildMapper.selectById(insureEntryChild.getInsureId());
//        if (!ObjectUtils.isEmpty(insureEntrChild1)) {
//            throw new BaseException("该员工已添加录入类型");
//        }
        if (i != 0) {
            return new JsonResult<>("200");
        }
        return new JsonResult<>("500");
    }

    //    根据被保人Id删除
    @Override
    public JsonResult<?> delete(InsureEntryChild insureEntryChild) {
        Long id = insureEntryChild.getInsureId();
        if (ObjectUtils.isEmpty(id)) {
            throw new BaseException("没有获取此id");
        }
        int i = insureEntryChildMapper.deleteById(id);
        if (i != 0) {
            return new JsonResult<>("200");
        }
        return new JsonResult<>("500");
    }
}